package com.ww.core.widget.scroll;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * 自定义控件 仿微信开屏.
 * @version 2013-1-8
 * @author wwang
 */
public class MyScrollLayout extends ViewGroup {
	private static final String TAG = "ScrollLayout";
	private VelocityTracker mVelocityTracker; // 用于判断甩动手势
	private static final int SNAP_VELOCITY = 600;
	private Scroller mScroller; // 滑动控制�?
	private int mCurScreen;
	private int mDefaultScreen = 0;
	private float mLastMotionX;
	private float mLastMotionY;
	private OnViewChangeListener mOnViewChangeListener;
	public static final int HORIZONTAL = 0;
	public static final int VERTICAL = 1;
	private int mOrientation = HORIZONTAL;

	public MyScrollLayout(Context context) {
		super(context);
		init(context);
	}

	public MyScrollLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		mCurScreen = mDefaultScreen;
		mScroller = new Scroller(context);
	}

	public int getmOrientation() {
		return mOrientation;
	}

	public void setmOrientation(int mOrientation) {
		this.mOrientation = mOrientation;
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if (changed) {
			int childLeft = 0;
			int childTop = 0;
			final int childCount = getChildCount();

			for (int i = 0; i < childCount; i++) {
				final View childView = getChildAt(i);
				if (childView.getVisibility() != View.GONE) {
					if (mOrientation == HORIZONTAL) {
						final int childWidth = childView.getMeasuredWidth();
						Log.e(TAG,
								childLeft + "  " + 0 + "  " + childLeft + childWidth + "  "
										+ childView.getMeasuredHeight());
						childView.layout(childLeft, 0, childLeft + childWidth, childView.getMeasuredHeight());
						childLeft += childWidth;
					} else {
						Log.e(TAG, "onLayout VERTICAL");
						final int childHeight = childView.getMeasuredHeight();
						Log.e(TAG, 0 + "  " + childTop + "  " + childView.getMeasuredWidth() + "  " + childTop
								+ childHeight);
						childView.layout(0, childTop, childView.getMeasuredWidth(), childTop + childHeight);
						childTop += childHeight;
					}
				}
			}
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		final int width = MeasureSpec.getSize(widthMeasureSpec);
		final int height = MeasureSpec.getSize(heightMeasureSpec);
		final int count = getChildCount();
		for (int i = 0; i < count; i++) {
			getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
		}
		if (mOrientation == HORIZONTAL) {
			scrollTo(mCurScreen * width, 0);
		} else {
			scrollTo(0, mCurScreen * height);
		}

	}

	public void snapToDestination() {
		if (mOrientation == HORIZONTAL) {
			final int screenWidth = getWidth();
			final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
			snapToScreen(destScreen);
		} else {
			final int screenHeight = getHeight();
			final int destScreen = (getScrollY() + screenHeight / 2) / screenHeight;
			snapToScreen(destScreen);
		}

	}

	public void snapToScreen(int whichScreen) {
		if (mOrientation == HORIZONTAL) {
			whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
			if (getScrollX() != (whichScreen * getWidth())) {
				final int delta = whichScreen * getWidth() - getScrollX();
				mScroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);
				mCurScreen = whichScreen;
				invalidate(); // Redraw the layout    
				if (mOnViewChangeListener != null) {
					mOnViewChangeListener.onViewChange(mCurScreen);
				}
			}
		} else {
			whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
			if (getScrollY() != (whichScreen * getHeight())) {
				final int delta = whichScreen * getHeight() - getScrollY();
				mScroller.startScroll(0, getScrollY(), 0, delta, Math.abs(delta) * 2);
				mCurScreen = whichScreen;
				invalidate(); // Redraw the layout    
				if (mOnViewChangeListener != null) {
					mOnViewChangeListener.onViewChange(mCurScreen);
				}
			}
		}

	}

	@Override
	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float x = event.getX();
		final float y = event.getY();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			if (mVelocityTracker == null) {
				mVelocityTracker = VelocityTracker.obtain();
				mVelocityTracker.addMovement(event);
			}
			if (!mScroller.isFinished()) {
				mScroller.abortAnimation();
			}
			mLastMotionX = x;
			mLastMotionY = y;
			break;

		case MotionEvent.ACTION_MOVE:
			int deltaX = (int) (mLastMotionX - x);
			int deltaY = (int) (mLastMotionY - y);
			if (isCanMove(deltaX, deltaY)) {
				if (mVelocityTracker != null) {
					mVelocityTracker.addMovement(event);
				}
				mLastMotionX = x;
				mLastMotionY = y;

				if (mOrientation == HORIZONTAL) {
					scrollBy(deltaX, 0);
				} else {
					scrollBy(0, deltaY);
				}

			}
			break;

		case MotionEvent.ACTION_UP:
			int velocityX = 0;
			int velocityY = 0;
			if (mVelocityTracker != null) {
				mVelocityTracker.addMovement(event);
				mVelocityTracker.computeCurrentVelocity(1000);
				velocityX = (int) mVelocityTracker.getXVelocity();
				velocityY = (int) mVelocityTracker.getYVelocity();
			}

			if (mOrientation == HORIZONTAL) {
				if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
					Log.e(TAG, "snap left");
					snapToScreen(mCurScreen - 1);
				} else if (velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount() - 1) {
					Log.e(TAG, "snap right");
					snapToScreen(mCurScreen + 1);
				} else {
					snapToDestination();
				}
			} else {
				if (velocityY > SNAP_VELOCITY && mCurScreen > 0) {
					Log.e(TAG, "snap top");
					snapToScreen(mCurScreen - 1);
				} else if (velocityY < -SNAP_VELOCITY && mCurScreen < getChildCount() - 1) {
					Log.e(TAG, "snap bottom");
					snapToScreen(mCurScreen + 1);
				} else {
					snapToDestination();
				}
			}

			if (mVelocityTracker != null) {
				mVelocityTracker.recycle();
				mVelocityTracker = null;
			}
			break;
		}
		return true;
	}

	private boolean isCanMove(int deltaX, int deltaY) {
		if (mOrientation == HORIZONTAL) {
			if (getScrollX() <= 0 && deltaX < 0) {
				return false;
			}
			if (getScrollX() >= (getChildCount() - 1) * getWidth() && deltaX > 0) {
				return false;
			}
		} else {
			if (getScrollY() <= 0 && deltaY < 0) {
				return false;
			}
			if (getScrollY() >= (getChildCount() - 1) * getHeight() && deltaY > 0) {
				return false;
			}
		}

		return true;
	}

	public void setOnViewChangeListener(OnViewChangeListener listener) {
		mOnViewChangeListener = listener;
	}

}
